package logic

import (
	"cartsrv/cartrpc"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddCartLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCartLogic {
	return &AddCartLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddCartLogic) AddCart(req *types.AddCartRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	cart, err := l.svcCtx.CartRpc.AddCart(l.ctx, &cartrpc.AddCartRequest{
		UserId:     int64(req.UserId),
		GoodsId:    int64(req.GoodsId),
		GoodsTitle: req.GoodsTitle,
		GoodsNum:   int64(req.GoodsNum),
		GoodsPrice: float32(req.GoodsPrice),
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: "商品不存在",
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    200,
		Message: "购物车商品添加成功",
		Data:    cart,
	}, nil
}
